# List of the object files of the present folder
OBJ= mod_linsolver_base.o \
     mod_iterativesolvers_base.o \
     mod_gmres.o \
     $(MOD_MUMPS_ITF).o \
     mod_linsolver.o \
     mod_newton.o

# General settings -----------------------------------

PWD:=$(shell pwd)
BUILDDIR:=$(LDGH_BUILDDIR)/linear-solvers

LIBS:=-lshared $(LIBS)
LDFLAGS:=$(LDFLAGS) -L$(LDGH_LIBDIR)

# VPATH includes:
#   PWD for the source files (included by default)
#   BUILDDIR for the .o prerequisites
#   LIBDIR for the libshared.a library
#   BINDIR for the final executable
VPATH:=$(BUILDDIR) $(LDGH_LIBDIR) $(LDGH_BINDIR)

# clear unfinished targets
.DELETE_ON_ERROR:
# define the phony targets
.PHONY: clean

# check waht to do with MUMPS
ifeq ($(USING_MUMPS),Yes)
  MOD_MUMPS_ITF=mod_mumpsintf

  XXX:=$(shell rm $(BUILDDIR)/mod_mumpsintf_dummy.o 2>/dev/null)
else
  MOD_MUMPS_ITF=mod_mumpsintf_dummy

  XXX:=$(shell rm $(BUILDDIR)/mod_mumpsintf.o 2>/dev/null)
endif

# Main targets ---------------------------------------

liblinsol.a: $(OBJ)
	-rm $(LDGH_LIBDIR)/liblinsol.a
	cd $(BUILDDIR) && $(AR) -r $(LDGH_LIBDIR)/liblinsol.a $(OBJ)
	cp $(BUILDDIR)/*.mod $(LDGH_LIBDIR)/

# General rule: all the .o depend on the libshared library
%.o: %.f90 libshared.a
	cd $(BUILDDIR) && \
	  $(FC) $(FFLAGS) $(MODFLAG)$(LDGH_LIBDIR) -c $(PWD)/$<

clean:
	-rm $(BUILDDIR)/*.o $(BUILDDIR)/*.ilo $(BUILDDIR)/*.mod \
	  $(LDGH_LIBDIR)/liblinsol.a $(LDGH_LIBDIR)/*.mod

# Specific dependencies: because of USE directives ---
# Notice: here we list only the prerequisites which are not already
# included in the libshared.a library, which is a prerequisite of all
# the .o files listed in this Makefile

mod_iterativesolvers_base.o: \
   mod_linsolver_base.o

mod_gmres.o: \
   mod_iterativesolvers_base.o

# We need a specific rule because there is an additional include
mod_mumpsintf.o: mod_mumpsintf.f90
mod_mumpsintf_dummy.o: mod_mumpsintf_dummy.f90
mod_mumpsintf.o mod_mumpsintf_dummy.o: \
  mod_linsolver_base.o
	cd $(BUILDDIR) && $(FC) $(FFLAGS) $(MODFLAG)$(LDGH_LIBDIR) \
          $(MUMPS_INCLUDE) -c $(PWD)/$(subst .o,.f90,$@)

mod_linsolver.o: \
   mod_linsolver_base.o \
   mod_iterativesolvers_base.o \
   mod_gmres.o \
   $(MOD_MUMPS_ITF).o
   
